package com.itheima.itheima_health.controller.admin.BookingController;

import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.entity.PageResult;
import com.itheima.itheima_health.entity.Result;
import com.itheima.itheima_health.pojo.OrderSetting;
import com.itheima.itheima_health.service.OrdersettingService;
import com.itheima.itheima_health.utils.POIUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置相关接口
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "预约设置相关接口")
public class OrdersettingController {
    @Autowired
    private OrdersettingService ordersettingService;

    /**
     * 批量导入预约设置
     * @param excelFile
     * @return
     */
    @ApiOperation("批量导入预约设置")
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        log.info("批量导入预约设置:{}",excelFile);
        try{
            List<String[]> strings = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] string : strings) {
                String orderDate = string[0];
                String number = string[1];

                OrderSetting orderSetting = new OrderSetting(new Date(orderDate),Integer.parseInt(number));
                orderSettings.add(orderSetting);
            }
            //通过dubbo远程调用服务实现数据批量导入到数据库
            ordersettingService.add(orderSettings);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (IOException e){
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据月份查询对应的预约设置数据
     * @param data
     * @return
     */
    @ApiOperation("根据月份查询对应的预约设置数据")
    @PostMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String data){
        log.info("根据月份查询对应的预约设置数据:{}",data);
        List<Map> mapList = ordersettingService.getOrderSettingByMonth(data);
        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,mapList);
    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @ApiOperation("根据指定日期修改可预约人数")
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        log.info("根据指定日期修改可预约人数:{}",orderSetting);
        ordersettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
